﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>添加图片</title>
    <link rel="stylesheet" href="~/static/common/layui/css/layui.css">
    <link rel="stylesheet" href="~/static/admin/css/style.css">
    <script src="~/static/common/layui/layui.js"></script>
    <script src="~/static/common/jquery-3.3.1.min.js"></script>
    <script src="~/static/common/vue.min.js"></script>
</head>
<body>
       
            <!--右侧-->
            <div class="right">
                <!-- 配置文件 -->
                <script type="text/javascript" src="~/static/common/ueditor/ueditor.config.js"></script>
                <!-- 编辑器源码文件 -->
                <script type="text/javascript" src="~/static/common/ueditor/ueditor.all.js"></script>
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>添加图片</legend>
                </fieldset>
                <div class="layui-form ">
                    <div class="layui-form-item">
                        <label class="layui-form-label">商品ID</label>
                        <div class="layui-input-inline">
                            <input type="text" id="productId" name="title" placeholder="" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">描述</label>
                        <div class="layui-input-inline">
                            <textarea type="text" cols="67" rows="5" name="key" id="pic_Desc"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <img class="layui-upload-img" id="demo1" width="200" height="200">
                            <p id="demoText"></p>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn" id="test1">上传图片</button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否主图</label>
                        <div class="layui-input-block">
                            <input type="checkbox" value="1" id="is_Master" name="top" lay-text="开启|关闭" lay-skin="switch">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">排序</label>
                        <div class="layui-input-inline">
                            <input type="text" id="pic_Order" name="title" placeholder="" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否有效</label>
                        <div class="layui-input-block">
                            <input type="checkbox" value="1" id="pic_Status" name="top" lay-text="开启|关闭" lay-skin="switch">
                        </div>
                    </div>                   
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" id="btn">立即提交</button>
                        </div>
                    </div>
                </div>
            </div>   
    <script src="~/static/admin/js/config.js"></script>
    <script src="~/static/admin/js/script.js"></script>
</body>
</html>
<script type="text/javascript">
    var tupian=null;
     layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/FileUpload/UploadImage'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                $('#demo1').attr('src', res.FilePath);
                tupian = res.FilePath;
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
            }
        });
     });
     $("#btn").click(function () {
        var datas = {};
        datas.ProductId = $("#productId").val();
        datas.Pic_Desc = $("#pic_Desc").val();
        datas.Pic_Url = tupian;
        if ($("#is_Master").get(0).checked) {
            datas.Is_Master = 1;
        } else {
            datas.Is_Master = 0;
        }
        datas.Pic_Order = $("#pic_Order").val();
        if ($("#pic_Status").get(0).checked) {
            datas.Pic_Status = 1;
        } else {
            datas.Pic_Status = 0;
        }

         $.ajax({
            url: "/Pic/AddUpdatePic",
            data: datas,
            type: "Post",
            success: function (data) {
                if (data.Success) {
                    //layer.msg("添加成功", { icon: 1, time: 2500, title: "提示框" });
                    layer.msg("添加成功");
                    window.location.href = '/Home/Price';
                }
                else {
                    layer.msg("添加失败", { icon: 1, time: 2500, title: "提示" });
                }
            }
        });
    });
</script>